package com.ustc.py.practice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ustc.py.practice.entity.Algorithm;
import com.ustc.py.practice.service.AlgorithmService;
import com.ustc.py.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/algorithm")
public class AlgorithmController {

    @Autowired
    private AlgorithmService algorithmService;

    @GetMapping("/{id}")
    public R getProById(@PathVariable("id")Integer id){
        return R.jsonify(200,"ok",algorithmService.query().eq("id",id).one());
    }

    @GetMapping("/chapter/{chapterId}")
    public R getProByChapterId(@PathVariable("chapterId")Integer chapterId){
        List<Algorithm> result = algorithmService.query().eq("chapter_id",chapterId).list();
        if(result.size()>0){
            return R.jsonify(200,"ok",result);
        }
        return R.jsonify(200,"无查询结果",null);
    }

    @GetMapping("/title/{title}")
    public R getProBytitle(@PathVariable("title") String title){
        QueryWrapper<Algorithm> wrapper = new QueryWrapper<>();
        wrapper.like("title",title);
        List<Algorithm> result = algorithmService.list(wrapper);
        return R.jsonify(200,"ok",result);
    }

}
